package com.in.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName TikUserDetails
 * @Description
 * @Author liulianjia
 * @Date 10:06 2022/7/7
 * @Version 1.0
 **/
@Data
@JsonIgnoreProperties({"enabled","accountNonExpired", "accountNonLocked", "credentialsNonExpired", "authorities"})
public class CustomerUserDetails implements Serializable, UserDetails {
    @Serial
    private static final long serialVersionUID = 3795011598734000565L;

    private String userId;

    private String username;

    private String password;

    private String nickname;

    private String avatar;

    private String gender;

    private LocalDateTime lastLogin;

    private String ipAddr;

    private LocalDateTime createTime;

    private List<String> permissions;

    public CustomerUserDetails() {
    }

    public CustomerUserDetails(String userId, String username, String password, String nickname,
                               String avatar, String gender, LocalDateTime lastLogin, LocalDateTime createTime,
                               List<String> permissions) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.nickname = nickname;
        this.avatar = avatar;
        this.gender = gender;
        this.lastLogin = lastLogin;
        this.createTime = createTime;
        this.permissions = permissions;
    }

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
        if (this.getPermissions() != null && !this.getPermissions().isEmpty()) {
            this.getPermissions().forEach(permission -> {
                grantedAuthorityList.add(new SimpleGrantedAuthority(permission));
            });
        }
        return grantedAuthorityList;
    }

    @Override
    @JsonIgnore
    public String getPassword() {
        return this.password;
    }
}
